'''
Created on Apr 11, 2012

@author: lavang
'''
from Interface.CoursesPage import xpathNewCourse,\
    xpathCourseName, xpathCategory, xpathLanguage, xpathActive, xpathShowCourse,\
    xpathPrice, xpathSubmit, xpathDeleteCourse, xpathSearchBox
from Interface.administratorPage import xpathCourse, xpathUsers
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from Interface.usersPage import xpathUser


def clickOnCourse(self):
    #browser=webdriver.Firefox()
    browser=self.browser
    element=browser.find_element_by_xpath(xpathCourse())
    element.click()
    browser.implicitly_wait(60)
    
def clickOnNewCourse(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathNewCourse())
    element.click()
    browser.implicitly_wait(60)
    
    
def createCourse(self,courseName,categories,language,price,statusCourse="active",showCourse="on"):
    #browser=webdriver.Firefox()
    browser=self.browser
    element=browser.find_element_by_xpath(xpathCourseName())
    element.clear()
    element.send_keys(courseName) # enter courser's name
    
    # select Category
    element=browser.find_element_by_xpath(xpathCategory())
    Select(element).select_by_visible_text(categories)
    
    # select language
    element=browser.find_element_by_xpath(xpathLanguage())
    Select(element).select_by_visible_text(language)
    
    # status of Course
   
    element=browser.find_element_by_xpath(xpathActive())
    if statusCourse=="active":
        if element.is_selected():
            pass
        else:
            element.click()
    elif statusCourse=="inactive":
        if element.is_selected():
                element.click()
    else:
        statusCourse=="active"
    
    # show course
    element=browser.find_element_by_xpath(xpathShowCourse())
    if showCourse=="on":
        if element.is_selected():
            pass
        else:
            element.click()
    else:
        if element.is_selected():
            element.click()
            
    # enter price
    element=browser.find_element_by_xpath(xpathPrice())
    element.clear()
    element.send_keys(price)
        
def clickOnSubmit(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathSubmit())
    element.click()    
    browser.implicitly_wait(30)  
    # return Course list
    browser=self.browser
    element=browser.find_element_by_xpath(xpathCourse())
    element.click()   
        
# Delete course
def deleteCourse(self,courseName):
    #browser=webdriver.Firefox()
    browser=self.browser
    element=browser.find_element_by_xpath(xpathDeleteCourse(courseName))
    element.click() 
    alert=browser.switch_to_alert()  
    alert.accept()     
    browser.refresh()
    browser.implicitly_wait(20)
    
# verify a course is created successfully
def verifyCourseCreatedSuccessfully(self,courseName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser: browser.find_element_by_xpath("//a[contains(text(),'"+courseName+"')]"))
        return True
    except:
        return False
    
# verify a course is delete successfully

def verifyCourseDeleted(self,courseName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//a[contains(text(),'"+courseName+"')]"))
        return True
    except:
        return False
    

def clickOnCourseName(self,courseName):
    browser=self.browser
    element=browser.find_element_by_xpath("//a[contains(text(),'"+courseName+"')]")
    element.click()
    
# Search a course 
def searchCourse(self,courseName):
#    browser = webdriver.Firefox()
    browser = self.browser
#    browser.switch_to_window("Update+courses_content")
    elem = browser.find_element_by_xpath("//table[@url='/administrator.php?ctg=courses&']/tbody/tr/td/div/input")
    elem.clear()
    elem.send_keys(courseName)
    elem.send_keys(Keys.RETURN)
    
# Verify a course is existed
def verifyACourseExist(self,courseName):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//a[text()='"+courseName+"']"))
        return True
    except:
        return False
    
# click on an User ( to edit infor)
def clickOnUserName(self,userName):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathUser(userName))
    element.click()